from hongruan import Address
from hongruan.BankAccount import BankAccount


class Bank:
    """银行类，管理多个用户账户"""
    MAX_CAPACITY = 100
    def __init__(self,bank_name:str):
        self.bank_name = bank_name
        self.accounts={}




    def open_account(self,card_type:int,name:str,password:int,address:Address,balance:int)->int:
        #开户
        # 检查用户库是否已满
        if len(self.accounts) >= self.MAX_CAPACITY:
            return 3

        # 创建临时账户以生成账号
        temp_account=BankAccount(
            card_type=card_type,
            name=name,
            password=password,
            address=address,
            balance=balance,
            bank_name=self.bank_name)

        account_number=temp_account.account
        #检查账号是否已经存在
        if account_number in self.accounts:
            return 2

        #添加账户到库中
        self.accounts[account_number]=temp_account
        return 1



    def deposit(self,account_number:str,amount:int)-> bool:
    #存钱
        if account_number not in self.accounts:
            return False
        self.accounts[account_number].balance += amount
        return True

    def withdraw(self,account_number:str,password:str,amount:int)->int:
    #取钱
        # 检查账号是否存在
        if account_number not in self.accounts:
            return 1
        account=self.accounts[account_number]
        # 检查密码是否正确
        if password!=account.password:
            return 2
        # 检查余额是否足够
        if amount>account.balance:
            return 3
        # 执行取款
        account.balance -= amount
        return 0
    def transfer(self,account_number1:str,password:str,account_number2:str,amount:int)->int:
    #转账
    # 检查账号1是否存在
        if account_number1 not in self.accounts:
            return 1
    # 检查账号2是否存在
        if account_number2 not in self.accounts:
            return 1
        account1 = self.accounts[account_number1]
        account2 = self.accounts[account_number2]

    # 检查密码是否正确
        if password != account1.password:
            return 2
        # 检查余额是否足够
        if amount > account1.balance:
            return 3
        #转账
        account1.balance -= amount
        account2.balance +=amount
        return 0


    def query_account(self,account_number:str,password:str)->None:
    # 查询账户功能
        if account_number not in self.accounts:
            print("该账号不存在")
            return
        account=self.accounts[account_number]

        if password != account.password:
            print("密码错误")
            return

        print(account)